import { MessageBoxService } from './../../components/messageBox/messageBoxService';
import { GrowlMessageType } from './../../components/messageBox/growMeaageType';
import { debounce } from "../utils/extended";
import * as R from "ramda";

export function OperationLock() {
    let pass = true;
    return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
        let method = descriptor.value;
        descriptor.value = async function (...args) {
            if (pass) {
                pass = false;
            } else {
                return;
            }
            try {
                let that = this;
                const result = await method.apply(that, args);
                return result;
            } catch (error) {
                throw error;
            } finally {
                pass = true;
            }
        }
    }
}


export function HandleExpectionErrorUseGrowMessage() {
    return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
        let method = descriptor.value;
        descriptor.value = async function (...args) {
            try {
                let that = this;
                const result = await method.apply(that, args);
                return result;
            } catch (error) {
                  MessageBoxService.current.Growl(GrowlMessageType.Error, error.message || error);
            }
        }
    }
}

export function Debounce(wait) {
    return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
        let method = descriptor.value;
        descriptor.value = debounce(method,wait || 2000,false)
    }
}

export function Once() {
    return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
        let method = descriptor.value;
        descriptor.value = R.once(method)
    }
}